package com.softserveinc.fragments.map;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;
import com.softserveinc.R;

public class BalloonOverlayView extends FrameLayout implements MapPopup {

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;

	private int offset = 10;

	public BalloonOverlayView(final Context context) {

		super(context);

		setVisibility(View.GONE);
		setPadding(10, 0, 10, offset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);

		View view = View.inflate(context, R.layout.balloon_overlay, layout);

		title = (TextView) view.findViewById(R.id.balloon_item_title);
		snippet = (TextView) view.findViewById(R.id.balloon_item_snippet);

		ImageView close = (ImageView) view.findViewById(R.id.close_img_button);

		close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(final View v) {
				hide();
			}
		});

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);
	}

	public void setOffset(final int offset) {
		this.offset = offset;
	}

	@Override
	public void showAt(final com.google.android.maps.MapView.LayoutParams params) {
		setLayoutParams(params);
		setVisibility(View.VISIBLE);
	}

	@Override
	public void hide() {
		setVisibility(View.GONE);
	}

	@Override
	public void setPopupOnClickListener(final OnClickListener clickListener) {
		findViewById(R.id.balloon_main_layout)
				.setOnClickListener(clickListener);
	}

	@Override
	public void setData(final OverlayItem item) {
		layout.setVisibility(VISIBLE);

		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			title.setText(item.getTitle());
		} else {
			title.setVisibility(GONE);
		}

		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			snippet.setText(item.getSnippet());
		} else {
			snippet.setVisibility(GONE);
		}
	}

}